package cn.mall.server.controller;

import cn.mall.common.utils.R;
import cn.mall.member.VO.RegistVO;

import cn.mall.common.vo.MemberEntity;
import cn.mall.server.openfeign.MemberFeignController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;

@Controller
public class RegistController {
    @Autowired
    private MemberFeignController controller;

    @PostMapping(value = "auth/regist", consumes="application/x-www-form-urlencoded")
    public String Regist(@Valid RegistVO registVO){
        System.out.println("menber = " + registVO);
        //todo 取数据库中查找注册的用户名和密码是否存在
        RegistVO registVO1 = new RegistVO(registVO.getName(), null, registVO.getTel());
        R r = controller.registAndSelect(registVO1);
        System.out.println("r = " + r);
        System.out.println("r123456 = " + r.getCode());
        if (r.getCode()!=0){
            System.out.println("menber = " + registVO);
            MemberEntity memberEntity=new MemberEntity();
            memberEntity.setUsername(registVO.getName());
            memberEntity.setPassword(registVO.getPassword());
            memberEntity.setMobile(registVO.getTel());
            R save =controller.save(memberEntity);
            System.out.println("save = " + save);
            return "redirect:http://localhost:10009/auth/login.html";
        }else {
            return "redirect:http://localhost:10009/auth/reg.html";
        }


    }
}
